有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

跨线程组的java JMeter BeanShell属性设置

我试图使用BeanShell后处理器在JMeter中计算两个事件之间的时间

在第一个块中,我获取时间并将其存储为属性。这是一个线程组。 然后在另一个线程组中,我有第二个BeanShell块。我犯了一个我无法理解的错误。我已将错误粘贴到这里。非常感谢您的提示和建议

下面是两段BeanShell代码:

第一个后处理器:

//Set the current time to the time_upload variable
long time_upload = prev.getTime(); // get POST Time

props.put("time_upload",(String.valueof(time_upload))); 
log.info("Time for Upload is: " + time_upload); // print difference to jmeter.log file

第二个后处理器:

String no_saved_carts = vars.get("no_saved_carts");
String no_saved_carts_trimmed = no_saved_carts.trim();

String temp_description = vars.get("description");
String temp_description_no_space = temp_description.trim();

String time_upload_local = props.get("time_upload");


if(temp_description_no_space.equals("</") || no_saved_carts_trimmed.equals("No Saved Carts Found")){
    vars.put("description","true");
} else{
    vars.put("description","false");
    //set the time to time_processing based on time_upload
    long time_processing_done = prev.getTime(); // get time 
    long time_upload_long = Long.parseLong(time_upload_local); // get HTTP Sampler 1 execution time from variable
    long delta = (time_processing_done - time_upload); // calculate difference
    log.info("Time difference is: " + delta + " ms"); // print difference to jmeter.log file
}

错误日志的相关部分:

2016/06/03 17:21:22 ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval   Sourced file: inline evaluation of: ``//Set the current time to the time_upload variable long time_upload = prev.getTi . . . '' : Error in method invocation: Static method valueof( long ) not found in class'java.lang.String' 
2016/06/03 17:21:22 WARN  - jmeter.extractor.BeanShellPostProcessor: Problem in BeanShell script org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval   Sourced file: inline evaluation of: ``//Set the current time to the time_upload variable long time_upload = prev.getTi . . . '' : Error in method invocation: Static method valueof( long ) not found in class'java.lang.String' 
2016/06/03 17:21:22 INFO  - jmeter.threads.JMeterThread: Thread is done: Upload Saved Cart Thread Group 1-1 
2016/06/03 17:21:22 INFO  - jmeter.threads.JMeterThread: Thread finished: Upload Saved Cart Thread Group 1-1 
2016/06/03 17:21:22 ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval   Sourced file: inline evaluation of: ``String temp_description = vars.get("description"); String no_saved_carts = vars. . . . '' : Typed variable declaration : Method Invocation Long.parseLong 
2016/06/03 17:21:22 WARN  - jmeter.extractor.BeanShellPostProcessor: Problem in BeanShell script org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval   Sourced file: inline evaluation of: ``String temp_description = vars.get("description"); String no_saved_carts = vars. . . . '' : Typed variable declaration : Method Invocation Long.parseLong 
2016/06/03 17:21:26 INFO  - jmeter.threads.JMeterThread: Thread is done: Check Upload Status 2-1 

共 (1) 个答案

  1. # 1 楼答案

    不能使用String将long转换为String。valueOf()方法,有以下选项:

    1. 如果仍然需要字符串,只需将行更改为将long转换为String,如下所示:

      props.put("time_upload", Objects.toString(time_upload,null)); 
      
    2. 摆脱long->;字符串,反之亦然,props是一个常见的java.util.Properties类实例,因此它存储Objects

      在第一个后处理器中:

      long time_upload = prev.getTime();
      props.put("time_upload", time_upload);
      

      在第二个后处理器中:

      long time_upload_long = props.get("time_upload"); // no need to cast from String
      
    3. 你可以使用bsh.shared namespace来保存任何对象——所有线程组都可以访问它

      在第一个后处理器中:

      bsh.shared.time_upload = prev.getTime();
      

      在第二个后处理器中:

      long time_upload = bsh.shared.time_upload
      

    您可以在jmeter中获得更多信息性错误消息。用try/catch块包围代码,记录Beanshell脚本错误时的日志文件,如:

    try {
        //your code here
    }
    catch (Throwable ex) {
        log.error("Something wrong", ex);
        throw ex;
    }
    

    有关JMeter和Beanshell的更多提示和技巧,请参见How to Use BeanShell: JMeter's Favorite Built-in Component